﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization;
using AutoChess;
using UnityEngine;
using UnityEngine.UI;
namespace AutoChess
{
    public class PlayerListItem : MonoBehaviour
    {
        [HideInInspector]
        public long playerGuid;
        public TextureKit icon;
        public Text playerName;
        public Text duan,level;
        public Slider slider;

        public GroupSpriteKit groupKit;
        public Text goldNum;
        public GameObject selfRoot,fightScrRoot,PreChooseScrRoot,deathState;
        public Transform itemFightRoot,itemPreChooseRoot;
        public HeroListItem prefab;
        private List<HeroListItem> cacheHeroList = new List<HeroListItem>();

        public void ResetPlayerInfo(ACPlayerData aCPlayerData)
        {
            playerGuid = aCPlayerData.GUID;
            icon.TextureName = UtilityHelper.GetShipIcon(aCPlayerData);
            playerName.text = aCPlayerData.NickName;
            var map = Table.Blo.AutoChessBlo.GetGradeById(aCPlayerData.Grade);
            duan.text = map.gradeName;
            slider.value = aCPlayerData.HP / 100f;
            goldNum.text = "x" + aCPlayerData.Gold;
            level.text ="lv"+ aCPlayerData.ShipLevel.ToString();
            selfRoot.SetActive(aCPlayerData.IsSeeSelf);
            groupKit.SetSpriteName(aCPlayerData.IsSeeSelf ? "UI_battle_zhandoutiao10" : "UI_battle_zhandoutiao2");
            deathState.SetActive(aCPlayerData.IsDeath);
        }

        private void ShowHeroList(List<HeroData> heros,Transform root)
        {
            for (int i = 0; i < heros.Count; i++)
            {
                var temp = GetHeroList(i, root);
                temp.InitHero(heros[i]);
                temp.gameObject.SetActive(true);
            }
            for (int i = heros.Count; i < cacheHeroList.Count; i++)
            {
                cacheHeroList[i].gameObject.SetActive(false);
            }
        }
        private HeroListItem GetHeroList(int i,Transform root)
        {
            if (cacheHeroList.Count > i)
            {
                cacheHeroList[i].transform.SetParent(root);
                return cacheHeroList[i];
            }
            var item = Instantiate<HeroListItem>(prefab, root);
            item.gameObject.SetActive(true);
            cacheHeroList.Add(item);
            return item;
        }
        public void SwitchToFightHero(ACPlayerData aCPlayerData) 
        {
            fightScrRoot.SetActive(true);
            PreChooseScrRoot.SetActive(false);
            ShowHeroList(aCPlayerData.GetHeroList(GridType.FightGrid,5), itemFightRoot);
        }

        public void SwitchToChooseHero(ACPlayerData aCPlayerData)
        {
            fightScrRoot.SetActive(false);
            PreChooseScrRoot.SetActive(true);
            ShowHeroList(aCPlayerData.GetHeroList(GridType.PreChooseGrid), itemPreChooseRoot);
        }

        public void SwitchToHero()
        {
            var playerData = AutoChessManager.Ins.cacheData.GetPlayerData(playerGuid);
            if (playerData.HP <= 0)
            {
                MessageBoxManager.Instance.CreatePopupWordTip(TableTextBlo.GetText("ac_player_death"));
                return;
            }
            AutoChessManager.Ins.SwitchToPlayer(playerGuid);
            Yunchang.PageManager.Instance.ClosePage("AutoChessPlayerListPage");
        }
    }
}